/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.engine.resource.find;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.bean.ResTypeBean;
import com.sinodata.bsm.center.cache.ResFindDefCache;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.vo.ResFindDef;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author tangli
 * @version 1.0
 * 
 * <p>
 * History:
 * 
 * Date Author Version Description
 * ---------------------------------------------------------------------------------
 * 2012-5-25 下午5:02:40 tangli 1.0 To create
 * </p>
 * 
 * @since
 * @see
 */
public class FindResGroup {
    private static ResFindDefCache resFindDefCache = SpringContextHolder.getBean(ResFindDefCache.class);
    private FindResBean parents;
    private Long resId;
    private boolean finish = true;
    private Map<Long, FindLog> findLogs = new LinkedHashMap<Long, FindLog>();

    /**
     * 
     */
    public FindResGroup(Long resId) {
        this.resId = resId;
    }

    public boolean isFinish() {
        return finish;
    }

    public void setFinish(boolean finish) {
        this.finish = finish;
    }

    public FindResBean getParents() {
        return parents;
    }

    public void setParents(FindResBean parents) {
        this.parents = parents;
    }

    public Map<Long, FindLog> getFindLogs() {
        return findLogs;
    }

    public void build() {
        ResBean resBean = ResBean.get(resId);
        parents = new FindResBean();
        parents.setRes(resBean.res());
        findLogs.clear();
        ResFindDef resFindDef = resFindDefCache.getForResType(resBean.res().getResTypeId());
        if (resFindDef != null) {
            FindLog log = new FindLog();
            log.setMessage("发现 " + resBean.res().getName() + " 的属性");
            findLogs.put(resFindDef.getId(), log);
        }
        List<ResFindDef> includes = resFindDefCache.getIncludeForResType(resBean.res().getResTypeId());
        for (ResFindDef def : includes) {
            ResTypeBean resTypeBean = ResTypeBean.get(def.getResTypeId());
            FindLog log = new FindLog();
            log.setMessage("发现 " + resTypeBean.resType().getName() + " 的子资源");
            findLogs.put(def.getId(), log);
        }
        build(parents);
    }

    public List<FindResBean> getAllBeans() {
        List<FindResBean> members = new ArrayList<FindResBean>();
        if (parents != null) {
            members.add(parents);
            depthTree(members, parents);
        }
        return members;
    }

    private void depthTree(List<FindResBean> members, FindResBean bean) {
        List<FindResBean> children = bean.getChildren();
        if (children != null && children.size() == 0) {
            return;
        }
        for (FindResBean b : children) {
            members.add(b);
            depthTree(members, b);
        }
    }

    private void build(FindResBean member) {
        ResBean[] children = ResBean.get(member.getRes().getId()).children();
        List<ResFindDef> includes = resFindDefCache.getIncludeForResType(member.getRes().getResTypeId());
        for (ResFindDef def : includes) {
            Long resTypeId = def.getResTypeId();
            for (ResBean bean : children) {
                if (bean.res().getResTypeId().equals(resTypeId)) {
                    FindResBean m = new FindResBean();
                    m.setRes(bean.res());
                    m.setParent(member);
                    member.addChild(m);
                    build(m);
                }
            }
        }
    }
}
